package com.etoak.controller;

import com.etoak.common.utils.DateUtils;
import com.etoak.common.utils.uuid.IdUtils;
import com.etoak.entity.CheckList;
import com.etoak.entity.KnowledgeProposal;
import com.etoak.service.IChecklistService;
import com.etoak.service.IKnowledgeProposalService;
import com.etoak.util.StringUtils;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Checklist库用户建议反馈Controller
 *
 * @Author zhixuebin
 * @Date 2022/5/13
 */
@RestController
@RequestMapping("/goertek/checklist/knowledge/proposal")
public class KnowledgeProposalController extends BaseController {

    @Autowired
    IKnowledgeProposalService knowledgeProposalService;

    @Autowired
    IChecklistService checklistService;

    /**
     * 提交反馈意见
     * @param knowledgeProposal
     * @return
     */
    @PostMapping("/addProposal")
    public AjaxResult addProposal(@RequestBody KnowledgeProposal knowledgeProposal){
        //拿取对应knowledgeId
        String knowledgeId = knowledgeProposal.getKnowledgeId();
        //根据id查询知识记录
        CheckList item = checklistService.getChecklistItemById(knowledgeId);
        int i;
        //非空判断
        if(StringUtils.isNotNull(item)){
            //设置主键
            String id = IdUtils.simpleUUID();
            knowledgeProposal.setId(id);
            knowledgeProposal.setProposalTime(DateUtils.getNowDate());
            i = knowledgeProposalService.addProposal(knowledgeProposal);
        }else{
            return AjaxResult.error("无对应数据");
        }
        if(i > 0){
            return AjaxResult.success("已提交" + i + "条反馈");
        }else{
            return AjaxResult.error("反馈提交失败!");
        }
    }

    /**
     * 获取反馈列表
     * @param knowledgeProposal
     * @return
     */
    @GetMapping("/getProposalList")
    public AjaxResult getProposalList(KnowledgeProposal knowledgeProposal){
        List<KnowledgeProposal> proposalList = knowledgeProposalService.getProposalList(knowledgeProposal);
        return AjaxResult.success(proposalList);
    }

    /**
     * 反馈意见审核(专家)
     * @param knowledgeProposal
     * @return
     */
    @PutMapping("/approve")
    AjaxResult approve(@RequestBody KnowledgeProposal knowledgeProposal){
        //查询记录
        KnowledgeProposal proposal = knowledgeProposalService.getProposalById(knowledgeProposal.getId());
        int i = 0;
        //非空判断
        if(StringUtils.isNotNull(proposal)){
            knowledgeProposal.setApproveTime(DateUtils.getNowDate());
            i = knowledgeProposalService.approve(knowledgeProposal);
        }
        if(i > 0){
            return AjaxResult.success("审核成功!");
        }
        return AjaxResult.error("审核失败!");
    }

    /**
     * 通过id查询详情
     * @param id
     * @return
     */
    @GetMapping("/getProposalById")
    public AjaxResult getProposalById(@RequestParam("id")String id){
        KnowledgeProposal proposal = knowledgeProposalService.getProposalById(id);
        if(proposal != null){
            return AjaxResult.success(proposal);
        }else{
            return AjaxResult.error("查无记录");
        }
    }

    /**
     * 逻辑删除
     * @param ids
     * @return
     */
    @GetMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable String[] ids){
        int i = 0;
        for(String id : ids){
            KnowledgeProposal proposal = knowledgeProposalService.getProposalById(id);
            proposal.setDelStatus("1");
            i = knowledgeProposalService.updateProposal(proposal);
        }
        return AjaxResult.success("已删除"+ i +"条记录",200);
    }

}
